/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "compiler/optimizer/code_generator/codegen.h"
#include "ecmascript_environment.h"
#include "plugins/ecmascript/compiler/ecmascript_call_params.h"

namespace ark::compiler {

bool Codegen::GenerateLoadObjectDynamic(Inst *inst)
{
    auto dstReg = ConvertRegister(inst->GetDstReg(), inst->GetType());
    auto objReg = ConvertRegister(inst->GetSrcReg(0), inst->GetInputType(0));
    auto keyReg = ConvertRegister(inst->GetSrcReg(1U), inst->GetInputType(1U));
    auto thisFunc = ConvertRegister(inst->GetSrcReg(2U), inst->GetInputType(2U));

    auto type = inst->CastToLoadObjectDynamic()->GetAccessType();
    auto mode = inst->CastToLoadObjectDynamic()->GetAccessMode();
    auto useIc = type == DynObjectAccessType::BY_NAME;

    EntrypointId id = EntrypointId::LOAD_OBJECT_DYNAMIC;

    if (type == DynObjectAccessType::BY_INDEX) {
        if (mode == DynObjectAccessMode::DICTIONARY) {
            id = EntrypointId::LOAD_OBJECT_DYNAMIC_BY_INDEX_DICTIONARY;
        }
    } else if (type == DynObjectAccessType::BY_NAME) {
        id = EntrypointId::LOAD_OBJECT_DYNAMIC_BY_NAME;
    }

    if (useIc) {
        CallFastPath(inst, id, dstReg, {}, objReg, keyReg, TypedImm(inst->GetPc()));
    } else {
        if (type != DynObjectAccessType::BY_INDEX) {
            CallFastPath(inst, id, dstReg, {}, objReg, keyReg, thisFunc);
        } else {
            CallFastPath(inst, id, dstReg, {}, objReg, keyReg);
        }
    }

    return true;
}

bool Codegen::GenerateStoreObjectDynamic(Inst *inst)
{
    auto dstReg = ConvertRegister(inst->GetDstReg(), inst->GetType());
    auto objReg = ConvertRegister(inst->GetSrcReg(0), inst->GetInputType(0));
    auto keyReg = ConvertRegister(inst->GetSrcReg(1U), inst->GetInputType(1U));
    auto valReg = ConvertRegister(inst->GetSrcReg(2U), inst->GetInputType(2U));
    auto thisFunc = ConvertRegister(inst->GetSrcReg(3U), inst->GetInputType(3U));

    auto type = inst->CastToStoreObjectDynamic()->GetAccessType();
    auto mode = inst->CastToStoreObjectDynamic()->GetAccessMode();
    auto useIc = type == DynObjectAccessType::BY_NAME;

    EntrypointId id = EntrypointId::STORE_OBJECT_DYNAMIC;

    if (type == DynObjectAccessType::BY_INDEX) {
        if (mode == DynObjectAccessMode::DICTIONARY) {
            id = EntrypointId::STORE_OBJECT_DYNAMIC_BY_INDEX_DICTIONARY;
        }
    } else if (type == DynObjectAccessType::BY_NAME) {
        id = EntrypointId::STORE_OBJECT_DYNAMIC_BY_NAME;
    }

    if (useIc) {
        CallFastPath(inst, id, dstReg, {}, objReg, keyReg, valReg, TypedImm(inst->GetPc()));
    } else {
        if (type != DynObjectAccessType::BY_INDEX) {
            CallFastPath(inst, id, dstReg, {}, objReg, keyReg, valReg, thisFunc);
        } else {
            CallFastPath(inst, id, dstReg, {}, objReg, keyReg, valReg);
        }
    }

    return true;
}

}  // namespace ark::compiler
